// 用来做请求封装
// const request = (url) => {
//   // fetch函数执行本来就返回Promise，所以不需要return new Promise()
//   return fetch("http://www.pudge.wang:3080/api" + url)
//     .then((response) => {
//       return response.json();
//     })
//     .then((res) => {
//       return res;
//     });
// };

// export default request;

import NProgress from "nprogress";
const baseURL = "http://www.pudge.wang:3080/api";

const http = {
  get(url, params) {
    if (params) {
      // params: {a: 3, b: 4}
      const search = new URLSearchParams(params).toString();
      url += "?" + search;
    }
    return fetch(baseURL + url)
      .then((response) => {
        return response.json();
      })
      .then((res) => {
        return res;
      });
  },
  post(url, data) {
    NProgress.start();
    return fetch(baseURL + url, {
      method: "post",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((response) => {
        NProgress.done();
        return response.json();
      })
      .then((res) => {
        return res;
      });
  },
};

export default http;
